
var campaigns = {
    
    init: function() {
        this.setupCampaignBlock();
    },
    
    setupCampaignBlock: function() {
        this.bindCreateCampaign();
        this.bindAddGroup();
        this.bindAccordion();
        this.bindActionLinks();
    },
    
    bindCreateCampaign: function() {
        $("#create_campaign").click(function() {
            var url = $(this).attr('href');
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    Jlib.showDialog({
                        title: 'Create Campaign',
                        content: data,
                        height: 500,
                        buttons: {
                            'Create Campaign': function() {
                                Jlib.submitDialog();
                            },
                            'Cancel': function() {
                                Jlib.closeDialog();
                            }
                        },
                        close: function() {
                            window.history.go(0);
                        }
                    });
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
            
        });
    },
    
    bindAddGroup: function() {
        $("#add_group").click(function() {
            var url = $(this).attr("href");
            
            $.get(url, function(data) {
                Jlib.showDialog({
                    title: 'Add Campaign Group',
                    content: data,
                    width: 450,
                    height: 300,
                    buttons: {
                        'Add Group': function() {
                            Jlib.submitDialog();
                        },
                        'Cancel': function() {
                            Jlib.closeDialog();
                        }
                    },
                    close: function() {
                        window.history.go(0);
                    }
                });
            });
        });
    },
    
    bindAccordion: function() {
        $(".accordion-group .remove-link").click(function(e) {
            e.preventDefault();
            
            if( !confirm("Remove Campaign Group and all Related Campaigns?") ) {
                return;
            }
            
            var url = $(this).attr("href");
            var self = $(this);
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    self.parents(".accordion-group").remove();
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
        
        $(".accordion-group tbody tr").click(function() {
            $(".accordion-group tbody tr").removeClass("selected");
            $(this).addClass("selected");
            
            var url = $(this).data('url');
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    $("#campaign_details").html( data );
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
    },
    
    bindActionLinks: function() {
        $(".edit_link").click(function(e) {
            e.preventDefault();
            
            var url = $(this).attr('href');
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    Jlib.showDialog({
                        title: 'Edit Campaign',
                        content: data,
                        height: 500,
                        buttons: {
                            'Save Changes': function() {
                                Jlib.submitDialog();
                            },
                            'Cancel': function() {
                                Jlib.closeDialog();
                            }
                        },
                        close: function() {
                            window.history.go(0);
                        }
                    });
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
        
        $(".delete_link").click(function(e) {
            e.preventDefault();
            
            if( confirm('Delete Campaign and related information?') ) {
                var url = $(this).attr('href');
                var self = $(this);
                
                $.get(url, function() {
                    self.parents("tr").remove();
                });
            }
        });
    }
};

$(document).ready(function() {
    campaigns.init();
});
 